<?php
class imageremote
{
	private $url;
	private $purl;
	private $size;
	private $type;
	private $error;
	private $mimetype;
	private $extar;
	private $comment;
	
	const GIF_SIGNATURE_LENGTH = 3;
	const GIF_VERSION_LENGTH = 3;
	const GIF_LSW_LENGTH = 2;
	const GIF_LSH_LENGTH = 2;
	
	const PNG_SIGNATURE_LENGTH = 8;
	const PNG_IHDR_LENGTH_LENGTH = 4;
	const PNG_IHDR_CHUNKTYPE_LENGTH = 4;
	const PNG_IHDR_IMW_LENGTH = 4;
	const PNG_IHDR_IMH_LENGTH = 4;
	
	const JPG_PARAM_LENGTH_LENGTH = 2;
	const JPG_DATA_PRECISION_LENGTH = 1;
	const JPG_IMW_LENGTH_LENGTH =2;
	const JPG_IMH_LENGTH_LENGTH  = 2;
	const JPG_NUM_COMPONENTS_LENGTH = 1;
	
	
	const  M_SOF0 =  0xC0;   /* Start Of Frame N */
	const  M_SOF1  =  0xC1;   /* N indicates which compression process */
	const  M_SOF2  =  0xC2;   /* Only SOF0-SOF2 are now in common use */
	const  M_SOF3  =  0xC3;
	const  M_SOF5  =  0xC5;   /* NB: codes C4 and CC are NOT SOF markers */
	const  M_SOF6  =  0xC6;
	const  M_SOF7  =  0xC7;
	const  M_SOF9  =  0xC9;
	const  M_SOF10  = 0xCA;
	const  M_SOF11  = 0xCB;
	const  M_SOF13  = 0xCD;
	const  M_SOF14  = 0xCE;
	const  M_SOF15  = 0xCF;
	const  M_SOI  =   0xD8;   /* Start Of Image  beginning of datastream */
	const  M_EOI  =   0xD9;   /* End Of Image  end of datastream */
	const  M_SOS  =   0xDA;   /* Start Of Scan  begins compressed data */
	const  M_APP0  =  0xE0;   /* Application-specific marker = type N */
	const  M_APP12  = 0xEC;   /*  we don't bother to list all 16 APPn's */
	const  M_COM  =   0xFE;   /* COMment */
		
	public function __construct() {
		
	}
	public function getUrl($input) {
		$this->purl = parse_url($input);
		print_r($this->purl);
		if($this->purl['scheme'] == 'http') {
			if(!($this->purl['port'])) {
				$this->purl['port'] = '80';
			}
			$this->error = '';
			imageremote::fetchImageInfo();
		} else {
			$this->error = '只支持HTTP';
		}	
	}
	private function fetchImageInfo() {
		$retval = 1;//默认返回值为1
		$fp = fsockopen($this->purl['host'],$this->purl['port'],$error,$errstr,30);
		
		if($fp) {
			//get headers
			fputs($fp,'GET'.$this->purl['path'].$this->purl['query'].'HTTP/1.1\r\n');
			fputs($fp,'HOST:'.$this->purl['host']);
			fputs($fp,'Connection:close\r\n\r\n');
			$line = trim(fgets($fp,128));
			while ($line) {
				if (preg_match("/^HTTP\/1\.[0-1] (3[0-9][0-9] .*)/", $line, $matches)) {
                    $this->errstr = $matches[1] . " not implemented yet!";
                    $retval = 0;
                }
                
                if (preg_match("/^HTTP\/1\.[0-1] (4[0-9][0-9] .*)/", $line, $matches)) {
                    $this->errstr = $matches[1];
                    $retval = 0;
                }
				
                if (preg_match("/^HTTP\/1\.[0-1] (5[0-9][0-9] .*)/", $line, $matches)) {
                    $this->errstr = $matches[1];
                    $retval = 0;
                }
                
                if (preg_match("/Content-Type: (.*)/", $line, $matches)) {
                    $tempmime = chop($matches[1]);
                }
               
                if (preg_match("/Content-Length: ([0-9]*)/", $line, $matches)) {
                   $tempfilesize = $matches[1];
                }
                $line = trim(fgets($fp,128));
			}
			
			if($retval) {
				if(preg_match("/image\/(gif|jpeg|x-jpeg|x-png|png)/",$tempmime)) {
					$this->mimetype = $tempmime;
					$this->size = $tempfilesize;
					
					switch ($this->mimetype) {
						case 'image/gif':
							imageremote::fetchAndParseGif($fp);
							$this->type = 1;
							break;
						case 'image/png':
						case 'image/x-png':
							imageremote::fetchAndParsePNG($fp);
							$this->type = 3;
							break;
						case 'image/jpeg':
						case 'image/x-jpeg':
							imageremote::fetchAndParseJPG($fp);
							$this->type = 2;
							break;
					}
				} else {
					$this->error = '不支持这种图片类型'.$this->mimetype;
					$retval = 0;
				}
			}
			
		} else {
			$this->error = "不能打开，$errstr($error)";
		}
		
		fclose($fp);
		return $retval;
	}
	
	protected  function fetchAndParseGif($fp) {
		if($fp) {
			$signarure = fread($fp,self::GIF_SIGNATURE_LENGTH );
			$version = fread($fp,self::GIF_VERSION_LENGTH );
			$wbytes = fread($fp,self::GIF_LSW_LENGTH );
			$hbytes = fread($fp,self::GIF_LSH_LENGTH );
			
			echo$this->width = imageremote::_htodecs($wbytes);
			echo$this->height = imageremote::_htodecs($hbytes);
			
			$this->extra = $signarure.$version;
		} else {
			$this->error = 'no socket';
		}
	}
	
	protected function fetchAndParsePNG($fp) {
		if($fp) {
			$signature = fread($fp,self::PNG_SIGNATURE_LENGTH );
			$ihdrl = fread($fp,self::PNG_IHDR_LENGTH_LENGTH );
			$ihdrct = fread($fp,self::PNG_IHDR_CHUNKTYPE_LENGTH );
			$wbytes = fread($fp,self::PNG_IHDR_IMW_LENGTH );
			$hbytes = fread($fp,self::PNG_IHDR_IMW_LENGTH );
			
			$this->width  = Image_Remote::_ntodecl($wbytes);
            $this->height = Image_Remote::_ntodecl($hbytes);

            $this->extra  = $signature; 
		} else {
			$this->error = 'no socket';
		}
	}
	
	protected function fetchAndParseJPG($fp) {
		 if (!$fd) {
            $this->error = "No socket.";
        } else {
            $done = 0;
            // first marker
            $c1 = ord(fread($fd, 1));
            $c2 = ord(fread($fd, 1));
            if ($c1 != 0xFF || $c2 != M_SOI) {
                $this->error = "Not a jpeg file?";
            } else { 
                while (!($done)) {
                    if ($m_sof && $m_com) {
                        $done = 1;
                    }
                    // find next marker
                    $marker = ord(fread($fd, 1));
                    while ($marker != 0xFF) {
                        $marker = ord(fread($fd, 1));
                    }   
                    do {
                        $marker = ord(fread($fd, 1));
                    } while ($marker == 0xFF);

                    SWITCH ($marker) {
                        case self::M_SOF0:
                        case self::M_SOF1:
                        case self::M_SOF2:
                        case self::M_SOF3:
                        case self::M_SOF5:
                        case self::M_SOF6:
                        case self::M_SOF7:
                        case self::M_SOF9: 
                        case self::M_SOF10:
                        case self::M_SOF11:
                        case self::M_SOF13:
                        case self::M_SOF14:
                        case self::M_SOF15:
                            $length    = fread($fd, self::JPG_PARAM_LENGTH_LENGTH);
                            $precision = fread($fd, self::JPG_DATA_PRECISION_LENGTH);
                            $hbytes    = fread($fd, self::JPG_IMH_LENGTH_LENGTH);
                            $wbytes    = fread($fd, self::JPG_IMW_LENGTH_LENGTH);
                            $components= fread($fd, self::JPG_NUM_COMPONENTS_LENGTH);
               
                            $this->width  = Image_Remote::_ntodecs($wbytes);
                            $this->height = Image_Remote::_ntodecs($hbytes);
                         
                            $this->extra  = "";
                            $m_sof = 1;
                            break; 
                         case self::M_COM:
                             // Get length of comment
                             $bytes = fread($fd, self::JPG_PARAM_LENGTH_LENGTH);
                             $length = Image_Remote::_chartodec($bytes);
                             $length -= self::JPG_PARAM_LENGTH_LENGTH;
                       
                             // Read comment
                             $this->comment = fread($fd, $length);
                             $m_com = 1;
                             break;
                        case M_SOS:  
                            $done = 1; 
                            break;

                        default:
                            $lbytes = fread($fd, self::JPG_PARAM_LENGTH_LENGTH);
                            $length = Image_Remote::_ntodecs($lbytes); 
                            if ($length < self::JPG_PARAM_LENGTH_LENGTH) {
                                $this->error ="Erronous parameter length.";
                                $done = 1;
                            } 
                       
                            $length -= self::JPG_PARAM_LENGTH_LENGTH;
                            fread($fd, $length);
                            break;
                    }    
                }
            }
        }
	}
	
	protected function _htodecl($bytes) {
        $b1 = ord($bytes[0]);
        $b2 = (ord($bytes[1])<<8);
        $b3 = (ord($bytes[2])<<16);
        $b4 = (ord($bytes[3])<<24);
        return($b1 + $b2 + $b3 + $b4);
    }
	
    protected  function _htodecs($bytes) {
        $b1 = ord($bytes[0]);
        $b2 = (ord($bytes[1]) << 8);
        return($b1 + $b2);
    }
	
     protected function _ntodecl($bytes) {
        $b1 = ord($bytes[3]);
        $b2 = (ord($bytes[2]) << 8);
        $b3 = (ord($bytes[1]) << 16);
        $b4 = (ord($bytes[0]) << 24);
        return($b1 + $b2 + $b3 + $b4); 
    }
	
    protected function _ntodecs($bytes) {
        $b1 = ord($bytes[1]);
        $b2 = (ord($bytes[0]) << 8 );
        return($b1 + $b2);
    }
    
    protected function _chartodec($bytes) {        
         $b1 = (ord($bytes[0]) << 8 );
         $b2 = ord($bytes[1]);
         return($b1 + $b2);
    }


	
	
	
}
?>